// https://www.lintcode.com/problem/task-scheduler/description

class Solution {
public:
    /**
     * @param tasks: the given char array representing tasks CPU need to do
     * @param n: the non-negative cooling interval
     * @return: the least number of intervals the CPU will take to finish all the given tasks
     */
     
    // 仔细分析题意可知，最后所消耗的时间主要受制于出现次数最多的那个字母，所以我们可以推导出，所消耗的时间为 count(字母最多出现次数) * k - (其他字母贡献)


    int leastInterval(string &tasks, int n) {
        map<char, int> mp;
        int cnt = 0;
        for (char t: tasks)
        {
            mp[t]++;
            cnt = max(cnt, mp[t]);
        }
        int result = (n + 1) * (cnt - 1);
        for (auto c: mp)
        {
            if (c.second == cnt)
                ++result;
        }
// Input
// "AAABCDEFGQWRT"
// 2
// Output
// 7
// Expected
// 13
        return max(result, (int)tasks.length()); //为什么要转换
    }
};